package cn.hzu.sotacy.service.impl.user;

import cn.hzu.sotacy.dao.course.CourseDao;
import cn.hzu.sotacy.model.course.Course;
import cn.hzu.sotacy.model.user.UserLearningCourse;
import cn.hzu.sotacy.dao.user.UserLearningCourseDao;
import cn.hzu.sotacy.response.ApiRestResponse;
import cn.hzu.sotacy.result.CodeResult;
import cn.hzu.sotacy.service.user.UserLearningCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2021-12-23
 */
@Service
public class UserLearningCourseServiceImpl extends ServiceImpl<UserLearningCourseDao, UserLearningCourse> implements UserLearningCourseService {

    @Resource
    CourseDao courseDao;

    /**
     * 添加用户学习课程
     * @param request  请求
     * @param response 回复
     * @return cn.hzu.sotacy.response.ApiRestResponse<cn.hzu.sotacy.model.user.UserLearningCourse>
     * @author Jasper Zhan
     * @date 2021/12/28 10:24
     */
    @Override
    public ApiRestResponse<UserLearningCourse> addUserLearningCourse(HttpServletRequest request, HttpServletResponse response) {

        String sUserId = request.getParameter("userId");
        String sCourseId = request.getParameter("courseId");

        if (sUserId == null || sUserId.equals(""))
            return ApiRestResponse.fail(CodeResult.EMPTY_USER_ID);

        if (sCourseId == null || sCourseId.equals(""))
            return ApiRestResponse.fail(CodeResult.EMPTY_COURSE_ID);

        int userId, courseId;

        try {
            userId = Integer.parseInt(sUserId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return ApiRestResponse.fail(CodeResult.ERROR_FORMAT_USER_ID);
        }

        try {
            courseId = Integer.parseInt(sCourseId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return ApiRestResponse.fail(CodeResult.ERROR_FORMAT_COURSE_ID);
        }


        UserLearningCourse userLearningCourse = new UserLearningCourse();
        userLearningCourse.setUserId(userId);
        userLearningCourse.setCourseId(courseId);

        save(userLearningCourse);

        return ApiRestResponse.success(CodeResult.SUCCESS_ADD_USER_LEARNING_COURSE, userLearningCourse);
    }

    /**
     * 获取用户学习课程
     * @param request 请求
     * @param response 回复
     * @return cn.hzu.sotacy.response.ApiRestResponse<java.util.List < cn.hzu.sotacy.model.user.UserLearningCourse>>
     * @author Jasper Zhan
     * @date 2021/12/28 15:24
     */
    @Override
    public ApiRestResponse<List<UserLearningCourse>> getUserLearningCourse(HttpServletRequest request, HttpServletResponse response) {

        String userId = request.getParameter("userId");

        if (userId == null || userId.equals(""))
            return ApiRestResponse.fail(CodeResult.EMPTY_USER_ID);

        QueryWrapper<UserLearningCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);

        List<UserLearningCourse> list = list(wrapper);

        if (list == null || list.size() == 0)
            return ApiRestResponse.fail(CodeResult.EMPTY_USER_LEARNING_COURSE);

        for (UserLearningCourse userLearningCourse: list) {
            Course course = courseDao.getById(userLearningCourse.getCourseId());
            userLearningCourse.setCourse(course);
        }

        return ApiRestResponse.success(CodeResult.SUCCESS_GET_USER_LEARNING_COURSE, list);
    }
}
